// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package com.example.ec2;

// snippet-start:[ec2.java2.describe_vpc.main]
// snippet-start:[ec2.java2.describe_vpc.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ec2.Ec2Client;
import software.amazon.awssdk.services.ec2.model.DescribeVpcsRequest;
import software.amazon.awssdk.services.ec2.model.DescribeVpcsResponse;
import software.amazon.awssdk.services.ec2.model.Vpc;
import software.amazon.awssdk.services.ec2.model.Ec2Exception;
import software.amazon.awssdk.services.ec2.paginators.DescribeVpcsIterable;
// snippet-end:[ec2.java2.describe_vpc.import]

/**
 * Before running this Java V2 code example, set up your development
 * environment, including your credentials.
 *
 * For more information, see the following documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class DescribeVPCs {
    public static void main(String[] args) {
        final String usage = """

                Usage:
                   <vpcId>

                Where:
                   vpcId - A  VPC ID that you can obtain from the AWS Management Console (for example, vpc-xxxxxf2f).\s
                """;

        if (args.length != 1) {
            System.out.println(usage);
            System.exit(1);
        }

        String vpcId = args[0];
        Region region = Region.US_EAST_1;
        Ec2Client ec2 = Ec2Client.builder()
                .region(region)
                .build();

        describeEC2Vpcs(ec2, vpcId);
        ec2.close();
    }

    public static void describeEC2Vpcs(Ec2Client ec2, String vpcId) {
        try {
            DescribeVpcsRequest request = DescribeVpcsRequest.builder()
                    .vpcIds(vpcId)
                    .build();

            DescribeVpcsIterable vpcsIterable = ec2.describeVpcsPaginator(request);
            vpcsIterable.stream()
                .flatMap(r -> r.vpcs().stream())
                .forEach(vpc -> System.out
                    .printf(
                        "Found VPC with id %s, " +
                            "vpc state %s " +
                            "and tenancy %s%n",
                        vpc.vpcId(),
                        vpc.stateAsString(),
                        vpc.instanceTenancyAsString()
                    )
                );

        } catch (Ec2Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
}
// snippet-end:[ec2.java2.describe_vpc.main]
